package com.foreveredu.propms.web.struts.action;

import java.util.List;
import java.util.Set;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.Building;
import com.foreveredu.propms.domain.BuildingType;
import com.foreveredu.propms.domain.District;
import com.foreveredu.propms.domain.PageModel;
import com.foreveredu.propms.domain.Role;
import com.foreveredu.propms.domain.SubDistrict;
import com.foreveredu.propms.domain.User;
import com.foreveredu.propms.service.IManagementService;
import com.foreveredu.propms.web.struts.actionform.BuildingActionForm;

public class BuildingAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	// 跳转到楼栋添加页面
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		// 获取当前登录用户
		User user = null;
		try {
			user = managementService.findUserByUsername(this.getPrincipal()
				.getUsername());
		} catch(ClassCastException e) {
			//用户长时间不操作，session丢失后，跳转到登录页面
			return mapping.findForward("login");
		}
		Set<Role> roles = user.getRoles();
		Role role_Admin = managementService.findRoleByName("ADMIN");
		Role role_ChargeMan = managementService.findRoleByName("CHARGEMAN");
		
		List<SubDistrict> subDistrictList = null;
		if(roles.contains(role_Admin)) {
			subDistrictList = managementService.findSubDistricts();
		} else if(roles.contains(role_ChargeMan)) {
			// 获取当前物业管理员所在的物业服务处
			District dis = user.getDistrict();
			subDistrictList = managementService.findSubDistrictsOfDistrict(dis);
		}
		
		request.setAttribute("subDistrictList", subDistrictList);
		
		//获取楼栋类型列表
		List<BuildingType> typesList = managementService.findBuildingTypes();
		request.setAttribute("typesList", typesList);

		return mapping.findForward("toAdd");
	}

	// 完成楼栋添加操作
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BuildingActionForm bdaf = (BuildingActionForm) form;
		
		//获取小区列表
		List<SubDistrict> subDistricts = managementService.findSubDistricts();
		request.setAttribute("subDistrictList", subDistricts);
		
		//获取楼栋类型列表
		List<BuildingType> typesList = managementService.findBuildingTypes();
		request.setAttribute("typesList", typesList);
		
		// 表单校验、
		if (bdaf.getBuildingId() == null || "".equals(bdaf.getBuildingId())) {
			request.setAttribute("buildingIdError", "请输入楼栋编号");

			return mapping.findForward("toAdd");
		} 
		if (bdaf.getName() == null || "".equals(bdaf.getName())) {
			request.setAttribute("buildingNameError", "请输入楼栋名称");

			return mapping.findForward("toAdd");
		}
		if (bdaf.getTypeId() == 0) {
			request.setAttribute("buildingTypeError", "请选择楼栋类型");

			return mapping.findForward("toAdd");
		}
		if (bdaf.getFloorCount() == 0) {
			request.setAttribute("buildingFloorCountError", "请输入楼栋类型");

			return mapping.findForward("toAdd");
		}
		if (bdaf.getSubDistrictId() == 0) {
			request.setAttribute("subDistrictIdError", "请选择所属小区！");

			return mapping.findForward("toAdd");
		}

		Building building = new Building();
		PropertyUtils.copyProperties(building, bdaf);

		SubDistrict subDistrict = managementService.findSubDistrictById(bdaf.getSubDistrictId());
		building.setSubDistrict(subDistrict);

		//获得楼栋类型
		BuildingType type = managementService.findBuildingTypeById(bdaf.getTypeId());
		building.setType(type);
		
		//保存楼栋
		managementService.saveBuilding(building);

		return mapping.findForward("toBuildingList");
	}

	// 楼栋翻页列表
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int offset = 0;
		String pageOffset = request.getParameter("pager.offset");// 获得翻页参数，即下一开始的纪录位置
		if (pageOffset != null && !pageOffset.equals("")) {
			offset = Integer.parseInt(pageOffset);
		}
		if (offset < 0) {
			return mapping.findForward("buildingList");
		}
		
		// 获取当前登录用户
		User user = null;
		try {
			user = managementService.findUserByUsername(this.getPrincipal()
				.getUsername());
		} catch(ClassCastException e) {
			//用户长时间不操作，session丢失后，跳转到登录页面
			return mapping.findForward("login");
		}
		Set<Role> roles = user.getRoles();
		Role role_ChargeMan = managementService.findRoleByName("CHARGEMAN");
		
		int districtId = 0;
		if(roles.contains(role_ChargeMan)) {
			// 获取当前物业管理员所在的物业服务处
			District dis = user.getDistrict();
			districtId = dis.getId();
		}
		
		PageModel pm = managementService.findBuildingList(offset, districtId);
		request.setAttribute("pm", pm);

		return mapping.findForward("buildingList");
	}

	// 跳转到楼栋修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BuildingActionForm bdaf = (BuildingActionForm) form;
		Building building = managementService.findBuildingById(bdaf.getId());
		request.setAttribute("building", building);
		
		//将楼栋的信息保存到表单中（基本数据类型）
		PropertyUtils.copyProperties(bdaf, building);
		
		//获得楼栋类型对象
		bdaf.setTypeId(building.getType().getId());
		
		//获得小区对象
		bdaf.setSubDistrictId(building.getSubDistrict().getId());
		
		//获得楼栋类型列表
		List<BuildingType> buildingTypesList = managementService.findBuildingTypes();
		request.setAttribute("buildingTypesList", buildingTypesList);
		
		// 将默认值保存到ActionForm中，以便在JSP表单中显示该值。
		bdaf.setSubDistrictId(building.getSubDistrict().getId());

		// 获取当前登录用户
		User user = null;
		try {
			user = managementService.findUserByUsername(this.getPrincipal()
				.getUsername());
		} catch(ClassCastException e) {
			//用户长时间不操作，session丢失后，跳转到登录页面
			return mapping.findForward("login");
		}
		Set<Role> roles = user.getRoles();
		Role role_Admin = managementService.findRoleByName("ADMIN");
		Role role_ChargeMan = managementService.findRoleByName("CHARGEMAN");
		
		List<SubDistrict> subDistrictList = null;
		if(roles.contains(role_Admin)) {
			subDistrictList = managementService.findSubDistricts();
		} else if(roles.contains(role_ChargeMan)) {
			// 获取当前物业管理员所在的物业服务处
			District dis = user.getDistrict();
			subDistrictList = managementService.findSubDistrictsOfDistrict(dis);
		}
		request.setAttribute("subDistrictList", subDistrictList);
		
		return mapping.findForward("buildingUpdate");
	}

	// 保存楼栋修改数据
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BuildingActionForm bdaf = (BuildingActionForm) form;
		
		
		if (bdaf.getName() == null || "".equals(bdaf.getName())) {
			request.setAttribute("buildingNameError", "请输入楼栋名称");

			return mapping.findForward("toUpdate");
		}
		if (bdaf.getTypeId() == 0) {
			request.setAttribute("buildingTypeError", "请选择楼栋类型");

			return mapping.findForward("toUpdate");
		}
		if (bdaf.getFloorCount() == 0 || "".equals(bdaf.getFloorCount())) {
			request.setAttribute("buildingFloorCountError", "请输入楼栋层数");

			return mapping.findForward("toUpdate");
		}
		if (bdaf.getSubDistrictId() == 0) {
			request.setAttribute("subDistrictIdError", "请输入所属小区！");

			return mapping.findForward("toUpdate");
		}
		Building building = managementService.findBuildingById(bdaf.getId());
		//将表单中的基本数据类型拷贝到building中
		PropertyUtils.copyProperties(building, bdaf);
		//读取表单中的楼栋类型，进行修改楼栋对象
		BuildingType buildingType = managementService.findBuildingTypeById(bdaf.getTypeId());
		building.setType(buildingType);
		
		SubDistrict subDistrict = managementService.findSubDistrictById(bdaf
				.getSubDistrictId());
		building.setSubDistrict(subDistrict);
		managementService.updateBuilding(building);

		return mapping.findForward("updateSave");
	}

	// 完成楼栋删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BuildingActionForm bdaf = (BuildingActionForm) form;
		managementService.deleteBuildingById(bdaf.getId());
		
		return mapping.findForward("delete");
	}

}
